use rusqlite::ToSql;
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
use serde::{Deserialize, Serialize};

#[derive(Debug, Default, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub(crate) enum ComponentV2rayProxyMode {
    #[default]
    Router,
    Global,
    Manual,
}

impl ToSql for ComponentV2rayProxyMode {
    fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
        match *self {
            ComponentV2rayProxyMode::Router => Ok(ToSqlOutput::Owned(rusqlite::types::Value::Text("Router".to_string()))),
            ComponentV2rayProxyMode::Global => Ok(ToSqlOutput::Owned(rusqlite::types::Value::Text("Global".to_string()))),
            ComponentV2rayProxyMode::Manual => Ok(ToSqlOutput::Owned(rusqlite::types::Value::Text("Manual".to_string()))),
        }
    }
}


impl FromSql for ComponentV2rayProxyMode {
    fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
        match value.as_str() {
            Ok("Router") => Ok(ComponentV2rayProxyMode::Router),
            Ok("Global") => Ok(ComponentV2rayProxyMode::Global),
            Ok("Manual") => Ok(ComponentV2rayProxyMode::Manual),
            _ => Err(FromSqlError::InvalidType),
        }
    }
}
